﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WeiXinPF.Common;
using System.Text;
using System.Data;

namespace WeiXinPF.Web.admin.product
{
    public partial class product_comment_approve : Web.UI.ManagePage
    {
        private BLL.wx_shop_product_comment bll = new BLL.wx_shop_product_comment();
        protected int totalCount;
        protected int page
        {
            get { return ViewState["page"] == null ? 0 : int.Parse(ViewState["page"].ToString()); }
            set { ViewState["page"] = value; }
        }
        protected int pageSize;

        protected int aduittypeid
        {
            get { return ViewState["type"]==null ? 0 : int.Parse(ViewState["type"].ToString()); }
            set { ViewState["type"] = value; }
        }



        protected string keywords = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            aduittypeid = MXRequest.GetQueryString("auduittype") == string.Empty ? aduittypeid : int.Parse(MXRequest.GetQueryString("auduittype"));
            
            this.pageSize = GetPageSize(10); //每页数量
            if (!this.IsPostBack) {

                if (aduittypeid != 0)
                {
                    ddlCategoryId.SelectedValue = aduittypeid.ToString();
                }
                this.page = MXRequest.GetQueryInt("page", 1);
                RptBind();
               
            }
        }

        #region 返回图文每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie(txtPageNum.Text), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion


        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

        }

        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("product_list_page_size", _pagesize.ToString(), 43200);
                }
            }
            Response.Redirect("product_comment_approve.aspx&auduittype="+aduittypeid+"&page=" + page.ToString());
        }

        #region 数据绑定=================================
        private void RptBind()
        {

            //this.page = MXRequest.GetQueryInt("page", 1);
            //列表显示

            Model.AduitStatus status = (Model.AduitStatus)aduittypeid;
            
            List<Model.wx_shop_product_comment> artDs = bll.GetApproveCommentsByPage(page,pageSize, status,out totalCount);
            
            foreach (Model.wx_shop_product_comment comment in artDs)
            {
                foreach (Model.wx_shop_product_commentimage image in comment.Images)
                {
                    if(image.ThumbPath.Trim()!=string.Empty && image.Path.Trim() != string.Empty)
                    { 
                        comment.ImagePath += "<img class=\"imagethumb\" src=\" " + image.ThumbPath+" \" onclick=\"showimage('"+image.Path+"')\" /> ";
                       
                    }
                }
            }

            this.rptList1.DataSource = artDs;
            this.rptList1.DataBind();


            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("product_comment_approve.aspx?auduittype=" + aduittypeid.ToString() + "&", "page={0}","__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected void btnpass_Click(object sender, EventArgs e)
        {
            approve(true);
        }

        protected void btnnopass_Click(object sender, EventArgs e)
        {
            approve(false);
        }

        protected void approve(bool pass)
        {
            Model.manager model = Session[MXKeys.SESSION_ADMIN_INFO] as Model.manager;
            Model.AduitStatus status = pass ? Model.AduitStatus.通过 : Model.AduitStatus.未通过;

            for (int i = 0; i < rptList1.Items.Count; i++)
            {
                CheckBox cb =(CheckBox)rptList1.Items[i].FindControl("chkId");
                int id = Convert.ToInt32(((HiddenField)rptList1.Items[i].FindControl("hidId")).Value);
                if (cb.Checked)
                {
                    bll.ApproveComment(id, status, model.id);
                }
            }
            RptBind();
        }

        protected string passstring(int status)
        {
            return ((Model.AduitStatus)status).ToString();
        }

        protected void linkbutton_Pass_Click(object sender, EventArgs e)
        {
            Model.manager model = Session[MXKeys.SESSION_ADMIN_INFO] as Model.manager;
            LinkButton lb = (LinkButton)sender;
            string id = lb.CommandArgument;
            bll.ApproveComment(int.Parse(id), Model.AduitStatus.通过, model.id);
            RptBind();
        }

        protected void linkbutton_unPass_Click(object sender, EventArgs e)
        {
            Model.manager model = Session[MXKeys.SESSION_ADMIN_INFO] as Model.manager;
            LinkButton lb = (LinkButton)sender;
            string id = lb.CommandArgument;
            bll.ApproveComment(int.Parse(id), Model.AduitStatus.未通过, model.id);
            RptBind();
        }

        protected void allpass_Click(object sender, EventArgs e)
        {
            Model.manager model = Session[MXKeys.SESSION_ADMIN_INFO] as Model.manager;
            bll.ApproveAllComment(model.id);
            RptBind();
        }

        public void ddlCategoryId_SelectedIndexChanged(object sender, EventArgs e)
        {
            aduittypeid = int.Parse(ddlCategoryId.SelectedValue.ToString());
            this.page = 1;
            RptBind();
        }
    }
}